﻿using System;
using System.Collections.Generic;
using Jayden.Dll.Web.Enums;
using Jayden.Dll.Web.HtmlControls;
using Jayden.Dll.Web.HtmlControls.Generics;

namespace Jayden.Dll.Web.Controls
{
	public class PlaceHolderDiv : Div
	{
		private HtmlControlLoader m_ControlLoader;

		public override void Load(System.Xml.XmlNode node, HtmlControlLoader loader)
		{
			ServerSafeId = true;
			m_ControlLoader = loader;
			base.Load(node, loader);
		}

		public override void ProcessHtmlRequest(HttpHandlerContext context)
		{
			LoadFromContext(context);
			base.ProcessHtmlRequest(context);
		}

		protected virtual void LoadFromContext(HttpHandlerContext context)
		{
			string id = Id;
			PageHtml pageHtml = Root as PageHtml;
			string pathsString = null;
			if (!string.IsNullOrWhiteSpace(id))
				pathsString = context.Uri[id];
			if(pathsString == null)
				pathsString = GetServerAttribute("default");
			if (!string.IsNullOrWhiteSpace(pathsString))
			{
				string[] paths = pathsString.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
				foreach (string path in paths)
				{
					string physicalPath = context.Context.Server.MapPath(path);
					HtmlControl control = HtmlControl.LoadControl(physicalPath, m_ControlLoader);
					AddControl(control);
				}
				Dictionary<string, HtmlElement> ids = new Dictionary<string,HtmlElement>();
				if(context.RenderMode == RenderMode.Production)
					HttpHandler.ObfuscateIds(ids, Root as HtmlElement);
				else
					HttpHandler.FixIds(ids, Root as HtmlElement);
			}
		}

		public override void ProcessBinRequest(HttpHandlerContext context)
		{
			LoadFromContext(context);
			base.ProcessBinRequest(context);
		}

		public override void ProcessCssRequest(HttpHandlerContext context)
		{
			LoadFromContext(context);
			base.ProcessCssRequest(context);
		}

		public override void ProcessJsonRequest(HttpHandlerContext context)
		{
			LoadFromContext(context);
			base.ProcessJsonRequest(context);
		}

		public override void ProcessJsRequest(HttpHandlerContext context)
		{
			LoadFromContext(context);
			base.ProcessJsRequest(context);
		}
	}

	public class PlaceHolderSection : Section
	{
		private HtmlControlLoader m_ControlLoader;

		public override void Load(System.Xml.XmlNode node, HtmlControlLoader loader)
		{
			ServerSafeId = true;
			m_ControlLoader = loader;
			base.Load(node, loader);
		}

		public override void ProcessHtmlRequest(HttpHandlerContext context)
		{
			LoadFromContext(context);
			base.ProcessHtmlRequest(context);
		}

		protected virtual void LoadFromContext(HttpHandlerContext context)
		{
			string id = Id;
			PageHtml pageHtml = Root as PageHtml;
			string pathsString = null;
			if (!string.IsNullOrWhiteSpace(id))
				pathsString = context.Uri[id];
			if (pathsString == null)
				pathsString = GetServerAttribute("default");
			if (!string.IsNullOrWhiteSpace(pathsString))
			{
				string[] paths = pathsString.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
				foreach (string path in paths)
				{
					string physicalPath = context.Context.Server.MapPath(path);
					HtmlControl control = HtmlControl.LoadControl(physicalPath, m_ControlLoader);
					AddControl(control);
				}
				Dictionary<string, HtmlElement> ids = new Dictionary<string, HtmlElement>();
				if (context.RenderMode == RenderMode.Production)
					HttpHandler.ObfuscateIds(ids, Root as HtmlElement);
				else
					HttpHandler.FixIds(ids, Root as HtmlElement);
			}
		}

		public override void ProcessBinRequest(HttpHandlerContext context)
		{
			LoadFromContext(context);
			base.ProcessBinRequest(context);
		}

		public override void ProcessCssRequest(HttpHandlerContext context)
		{
			LoadFromContext(context);
			base.ProcessCssRequest(context);
		}

		public override void ProcessJsonRequest(HttpHandlerContext context)
		{
			LoadFromContext(context);
			base.ProcessJsonRequest(context);
		}

		public override void ProcessJsRequest(HttpHandlerContext context)
		{
			LoadFromContext(context);
			base.ProcessJsRequest(context);
		}
	}
}
